package com.cn.wanxi.io;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.IO;
import com.sun.corba.se.impl.orbutil.ObjectUtility;

import java.io.*;
import java.util.Scanner;

/**
 * 公司信息io流
 */
public class CompanyIO {
    String spllit = "@#@";


    public void save(File file) {

//        File file = create();
//        现在要保存数据，应该用输入还是输出流？
//        Scanner scanner=new Scanner(System.in);
//        抽象类，一般情况下  需要其他类去对当前抽象进行实现
        try {
            OutputStream outputStream = new FileOutputStream(file);
            CompanyDao companyDao = new CompanyDao();
//字符串转换为字节数组
            String logo = companyDao.getLogo();
//            outputStream.write(logo.getBytes(), 10, 20);
            outputStream.write(logo.getBytes());
            outputStream.write(spllit.getBytes());
            outputStream.write(companyDao.getPhone().getBytes());
            outputStream.write(spllit.getBytes());
            outputStream.write(companyDao.getNet().getBytes());
            outputStream.write(spllit.getBytes());
            outputStream.write(companyDao.getEmail().getBytes());
            outputStream.write(spllit.getBytes());
            outputStream.write(companyDao.getAddress().getBytes());
            outputStream.write(spllit.getBytes());
            outputStream.write(companyDao.getLinePhone().getBytes());
            outputStream.write(spllit.getBytes());
            outputStream.write(companyDao.getVersion().getBytes());
            outputStream.write(spllit.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    /**
     * 封装数据：
     * 1必须有数据
     * 2.数据来源于读取
     *
     * @return
     */
    public CompanyModel getModel() {
        File file = IO.create("company.txt");//创建文件
        save(file);//保存数据
        String result =IO.read(file);//读取数据
//        regex 是正则表达式
        String[] results = result.split(spllit);
//        for (String data : results
//        ) {
//            System.out.println(data);
//        }

        for (int i = 0; i < results.length; i++) {
            System.out.println(results[i]);
        }
        CompanyModel companyModel = new CompanyModel();
        companyModel.setLogo(results[0]);
        companyModel.setPhone(results[1]);
        companyModel.setNet(results[2]);
        companyModel.setEmail(results[3]);
        companyModel.setAddress(results[4]);
        companyModel.setLinePhone(results[5]);
        companyModel.setVersion(results[6]);
        return companyModel;
    }

}
